package weaver.interfaces.cese.duss.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.engine.cese.duss.sms.util.UPMutil;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-11-18
 * @Description: 发送红豆短信接口
 * @ModifyLog:
 **/
@WebServlet("/hodo/sendSMS")
public class SendSMSServiceImpl extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        super.doPost( req, res );
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String phone = Util.null2String( req.getParameter( "phone" ) );
        String content = Util.null2String( req.getParameter( "content" ) );
        PrintWriter out = res.getWriter();
        res.setHeader( "Content-type", "text/html;charset=UTF-8" );
        BaseBean baseBean = new BaseBean();
        String massURL = baseBean.getPropValue( "hodo","url");
        String username =  baseBean.getPropValue( "hodo","username");
        String password =  baseBean.getPropValue( "hodo","password");
        String param = UPMutil.getMassJsonContent( phone, content );
        Map<String, String> headers = new HashMap<>();
        String authorization = UPMutil.generateAuthorization( username, password );
        headers.put( "accept", "application/json" );
        headers.put( "Content-Type", "application/json; charset=utf-8" );
        headers.put( "Authorization", authorization );
        String encode = "UTF-8";
        try {
            String result = UPMutil.httpPostRaw( massURL, param, headers, encode );
            RecordSet rs = new RecordSet();
            JSONObject resultJson = JSONObject.parseObject( result );
            String code = resultJson.getString( "code" );
            if ("0".equals( code )) {
                String status = "0";
                String curtime = DateUtil.getFullDate();
                UUID MIT_UUID = UUID.randomUUID();
                String uuid = MIT_UUID.toString(); //唯一标志uuid
                String sql = "insert into uf_smscode (uuid,smscode,mobile,status,systemdate) values (?,?,?,?,?)";
                rs.executeUpdate( sql, new Object[]{uuid, content,phone,status, curtime} );
            }
            out.print( resultJson.toJSONString() );
        } catch (Exception e) {
            e.printStackTrace();
            out.print( "request error" );
        }
    }
}
